package com.hk.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.hk.commons.CommonResult;
import com.hk.model.*;
import com.hk.model.vo.Result;
import com.hk.model.vo.ResultVO;
import com.hk.service.*;
import com.hk.utils.JedisUtil;
import com.hk.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: aerfazhe
 * @Date: 2021/7/13 9:51
 * @Statement: 网站展示API
 */
@RestController
@RequestMapping("/qiantai")
public class WebsiteController {

    @Autowired
    private INavService iNavService;

    @Autowired
    private ICompanyMessageService iCompanyMessageService;

    @Autowired
    private INewsService iNewsService;
    @Autowired
    private INewsTypeService iNewsTypeService;

    @Autowired
    private IProductService iProductService;
    @Autowired
    private IProductMenuService iProductMenuService;

    @Autowired
    private IOnlineMessageService iOnlineMessageService;

    @Autowired
    private IContactUsService iContactUsService;


    /**
     * 共有
     * @return
     */
    @GetMapping("/mutual")
    public Map<String, Object> mutual() {
        Jedis jedis = JedisUtil.getJedis();
//        查询所有导航信息
        List<Nav> navList = new ArrayList<>();
        String navList1 = jedis.get("navList");
        if (navList1 == null || navList1.length() == 0) {
            System.out.println("从数据库获取导航栏");
            navList = iNavService.findAll(1, 0, null);
            jedis.set("navList", JSON.toJSONString(navList));
        } else {
            navList = JSON.parseArray(navList1, Nav.class);
            System.out.println("从Redis获取的导航栏");
        }

//        查询公司信息默认0启用
        CompanyMessage companyMessage = new CompanyMessage();
        companyMessage.setState(0);
//        从redis获取公司信息
        CompanyMessage companyMessage1;
        String company = jedis.get("company");
        if (company == null || company.length() == 0) {
            System.out.println("从数据库获取公司信息");
            companyMessage1 = iCompanyMessageService.findByState(companyMessage);
            jedis.set("company", JSON.toJSONString(companyMessage1));
        } else {
            companyMessage1 = JSON.parseObject(company, CompanyMessage.class);
            System.out.println("从Redis获取的公司信息");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("status", ResultCodeEnum.SUCCESS.getCode());
        map.put("message", ResultCodeEnum.SUCCESS.getMessage());
        map.put("commpany",companyMessage1);
        map.put("navList",navList);
        return map;
    }

    /**
     *  首页
     */
    @GetMapping("/home")
    public Map<String,Object> home() {
//        查询浏览次数大于并=600的新闻
        List<News> newsList = iNewsService.findByHits(600);
//        获取产品图片信息
        List<Product> productList = iProductService.findAllImg();
        Map<String,Object> map = new HashMap<>();
        map.put("status", ResultCodeEnum.SUCCESS.getCode());
        map.put("message", ResultCodeEnum.SUCCESS.getMessage());
        map.put("productList",productList);
        map.put("newsList",newsList);
        return map;
    }

    /**
     * 新闻类型
     */
    @GetMapping("/newsType")
    public ResultVO newsType() {
//      获取所有新闻类型
        List<NewsType> newsTypeList = iNewsTypeService.findAll(1,0,null);
        if (!newsTypeList.isEmpty()) {
            ResultVO resultVO = CommonResult.successResultVO(newsTypeList.size(), newsTypeList);
            return resultVO;
        }
        ResultVO resultVO = CommonResult.nullResultVO(newsTypeList.size(), newsTypeList);
        return resultVO;
    }

    /**
     * 分页查询新闻
     * 模糊查询
     * @param page
     * @param limit
     * @param news
     * @return
     */
    @GetMapping("/newsTrends")
    public Map<String, Object> newsTrends(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10")Integer limit,
            News news
    ) {
//      获取所有新闻类型
        List<News> newsList = iNewsService.findAll(page,limit,news);
        PageInfo<News> newsPageInfo = new PageInfo<>(newsList);
        Map<String,Object> map = new HashMap<>();
        if (!newsList.isEmpty()) {
            map.put("status", ResultCodeEnum.SUCCESS.getCode());
            map.put("message", ResultCodeEnum.SUCCESS.getMessage());
            map.put("pageNum", newsPageInfo.getPageNum());
            map.put("pages",newsPageInfo.getPages());
            map.put("total",newsList.size());
            map.put("list",newsList);
            return map;
        }
        map.put("status", ResultCodeEnum.NULL.getCode());
        map.put("message", ResultCodeEnum.NULL.getMessage());
        map.put("pageNum", newsPageInfo.getPageNum());
        map.put("pages",newsPageInfo.getPages());
        map.put("total",newsList.size());
        map.put("list",newsList);
        return map;
    }

    /**
     *  通过Id查询本身及上下篇
     * @param id
     * @return
     */
    @GetMapping("/newsFindById/{id}")
    public ResultVO newsFindById(@PathVariable Integer id) {
//      获取所有新闻类型
        List<News> newsList = iNewsService.findByIds(id);
        if (!newsList.isEmpty()) {
            ResultVO resultVO = CommonResult.successResultVO(newsList.size(),newsList);
            return resultVO;
        }
        ResultVO resultVO = CommonResult.nullResultVO(0,null);
        return resultVO;
    }

    /**
     *  通过Id修改浏览次数
     * @param news
     * @return
     */
    @PostMapping("/newsUpdate")
    public Result newsUpdate(News news) {
//      获取当前新闻Id
        int update = iNewsService.updateHits(news);
        if (update != 1) {
            Result result = CommonResult.errorResult(null);
            return result;
        }
        Result result = CommonResult.successResult(null);
        return result;
    }

    /**
     * 查询产品菜单
     * @return
     */
    @GetMapping("/productMenu")
    public ResultVO productMenu() {
//      获取所有新闻类型
        List<ProductMenu> productMenuList = iProductMenuService.findAll(1,0,null);
        if (!productMenuList.isEmpty()) {
            ResultVO resultVO = CommonResult.successResultVO(productMenuList.size(),productMenuList);
            return resultVO;
        }
        ResultVO resultVO = CommonResult.nullResultVO(0,null);
        return resultVO;
    }

    /**
     * 分页查询产品
     * 根据菜单查询产品
     * @param page
     * @param limit
     * @param product
     * @return
     */
    @GetMapping("/productDisplay")
    public Map<String, Object> productDisplay(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10")Integer limit,
            Product product
    ) {
//      分页获取所有产品
        List<Product> productList = iProductService.findAll(page, limit,product);
        PageInfo<Product> productPageInfo = new PageInfo<>(productList);
        int count = iProductService.count(-1, -1,product);
        Map<String,Object> map = new HashMap<>();
        if (!productList.isEmpty()) {
            map.put("status", ResultCodeEnum.SUCCESS.getCode());
            map.put("message", ResultCodeEnum.SUCCESS.getMessage());
            map.put("pageNum", productPageInfo.getPageNum());
            map.put("pages",productPageInfo.getPages());
            map.put("total",count);
            map.put("list",productList);
            return map;
        }
        map.put("status", ResultCodeEnum.NULL.getCode());
        map.put("message", ResultCodeEnum.NULL.getMessage());
        map.put("pageNum", productPageInfo.getPageNum());
        map.put("pages",productPageInfo.getPages());
        map.put("total",count);
        map.put("list",productList);
        return map;
    }

    @GetMapping("/productFindById/{id}")
    public ResultVO productFindById(@PathVariable Integer id) {
//      获取所有新闻类型
        List<Product> productList = iProductService.findByIds(id);
        if (!productList.isEmpty()) {
            ResultVO resultVO = CommonResult.successResultVO(productList.size(),productList);
            return resultVO;
        }
        ResultVO resultVO = CommonResult.nullResultVO(0,null);
        return resultVO;
    }

    @PostMapping("/onlineMessage")
    public Result onlineMessage(@RequestBody OnlineMessage onlineMessage) {
        int save = iOnlineMessageService.save(onlineMessage);
        if (save != 1) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        Result result = CommonResult.successResult(null);
        return result;
    }

    @GetMapping("/contactUs")
    public Result contactUs() {
//        查询启动的联系我们信息0
        ContactUs contactUs = iContactUsService.findByState(0);
        if (contactUs != null) {
            Result result = CommonResult.successResult(contactUs);
            return result;
        }
        Result result = CommonResult.nullResult(null);
        return result;

    }



























}
